﻿<!--
* 开源版本请务必保留此注释头信息，若删除捷码开源〔GEMOS〕官方保留所有法律责任追究！
* 本软件受国家版权局知识产权以及国家计算机软件著作权保护（登记号：2018SR503328）
* 不得恶意分享产品源代码、二次转售等，违者必究。
* Copyright (c) 2020 gemframework all rights reserved.
* http://www.gemframework.com
* 版权所有，侵权必究！
-->
<!DOCTYPE html>
<html lang="zh-cn" class="fullscreen-bg" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:replace="common/links :: common_header(~{::title},~{},~{})">
    <title>示例模块</title>
</head>
<body>
<!-- 页面加载loading -->
<div class="page-loading">
    <div class="ball-loader">
        <span></span><span></span><span></span><span></span>
    </div>
</div>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">示例名称:</label>
                        <div class="layui-input-inline">
                            <input name="name" class="layui-input" placeholder="请输入示例名称"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">示例内容:</label>
                        <div class="layui-input-inline">
                            <input name="content" class="layui-input" placeholder="请输入示例内容"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">添加日期:</label>
                        <div class="layui-input-inline">
                            <input name="createDateRange" class="layui-input icon-date" placeholder="选择日期范围"
                                   autocomplete="off"/>
                        </div>
                    </div>
                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="searchBtn" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>&nbsp;
                        <button shiro:hasPermission="demo:export" id="exportBtn" class="layui-btn icon-btn" type="button">
                            <i class="layui-icon">&#xe67d;</i>导出
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBar">
    <a shiro:hasPermission="demo:update" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a shiro:hasPermission="demo:delete" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表格状态列 -->
<script type="text/html" id="demoStatus">
    <p>{{d.status==0?'启用':'禁用'}}</p>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="formDialog">
    <form id="dataForm" lay-filter="dataForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">示例名称:</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入示例名称" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">示例内容:</label>
            <div class="layui-input-block">
                <input name="content" placeholder="请输入示例内容" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">数据状态:</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="0" title="启用 " checked/>
                <input type="radio" name="status" value="1" title="禁用"/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="saveBtn" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>

<!-- js部分1 -->
<div th:replace="common/scripts :: common"></div>
<div th:replace="common/scripts :: custom"></div>
<div th:replace="common/shiro :: demoShiroTag"></div>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'laydate', 'tableX','admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var laydate = layui.laydate;
        var tableX = layui.tableX;
        var admin = layui.admin;

        /* 渲染表格 */
        var cols = [
            {type: 'checkbox'},
            {field: 'name', title: '示例名称',align: 'center'},
            {field: 'content', title: '示例内容',align: 'center'},
            {field: 'status', title: '数据状态', sort: true, templet: '#demoStatus', align: 'center'},
            {
                field: 'createTime', title: '操作时间', templet: function (d) {
                    return util.toDateString(d.createTime);
                }, sort: true
            },
            {title: '操作', toolbar: '#tableBar', align: 'center', minWidth: 200}
        ];

        var insTb = table.render({
            elem: '#dataTable',
            url: 'demo/page',
            page: true,
            toolbar: [shiroToolbar(shiroSave,shiroDelete)].join(''),
            cellMinWidth: 100,
            cols: [cols],
            done: function () {
                // 绑定鼠标右键
                tableX.bindCtxMenu('dataTable', shiroBindCtxMenu(shiroDelete,shiroUpdate));
            }
        });

        /* 表格工具条点击事件 */
        table.on('tool(dataTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj.data);
            } else if (obj.event === 'reset') { // 重置密码
                resetPsw(obj);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(dataTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('dataTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }
        });

        // 显示编辑弹窗
        function showEditModel(mRole) {
            admin.open({
                type: 1,
                area: '800px',
                title: (mRole ? '修改' : '添加'),
                content: $('#formDialog').html(),
                shadeClose:true,
                success: function (layero, dIndex) {
                    form.val('dataForm', mRole);  // 回显数据
                    form.on('submit(saveBtn)', function (data) {
                        $.post(mRole?'demo/update':'demo/save', data.field, function (res) {
                            if (res.code == 0) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload({page: {curr: 1}});
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });
                }
            });
        }

        /* 导出excel */
        $('#exportBtn').click(function () {
            var checkRows = table.checkStatus('dataTable');
            if (checkRows.data.length === 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                tableX.exportDataX({
                    cols: insTb.config.cols,
                    data: checkRows.data,
                    fileName: '导出数据'
                });
            }
        });

        /* 删除 */
        window.doDel = function(obj){
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                $.post('demo/delete', {
                    id: obj ? obj.id : '',
                    ids: obj.ids ? obj.ids.join(',') : ''
                }, function (res) {
                    layer.close(loadIndex);
                    if (res.code === 0) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }

        /* 渲染时间选择 */
        laydate.render({
            elem: 'input[name="createDateRange"]',
            type: 'date',
            range: true,
            trigger: 'click'
        });
        laydate.render({
            elem: 'input[name="updateDateRange"]',
            type: 'date',
            range: true,
            trigger: 'click'
        });
        laydate.render({
            elem: 'input[name="createTimeRange"]',
            type: 'date',
            range: true,
            trigger: 'click'
        });
        laydate.render({
            elem: 'input[name="updateTimeRange"]',
            type: 'date',
            range: true,
            trigger: 'click'
        });
        /* 表格搜索 */
        form.on('submit(searchBtn)', function (data) {
            if(data.field.updateDateRange) {
                var searchDate = data.field.updateDateRange.split(' - ');
                data.field.updateStartDate = searchDate[0];
                data.field.updateEndDate = searchDate[1];
            }else{
                data.field.updateStartDate = null;
                data.field.updateEndDate = null;
            }
            if(data.field.createDateRange) {
                var searchDate = data.field.createDateRange.split(' - ');
                data.field.createStartDate = searchDate[0];
                data.field.createEndDate = searchDate[1];
            }else{
                data.field.createStartDate = null;
                data.field.createEndDate = null;
            }
            if(data.field.updateTimeRange) {
                var searchDate = data.field.updateDateRange.split(' - ');
                data.field.updateStartTime = searchDate[0];
                data.field.updateEndTime = searchDate[1];
            }else{
                data.field.updateStartTime = null;
                data.field.updateEndTime = null;
            }
            if(data.field.createTimeRange) {
                var searchDate = data.field.createDateRange.split(' - ');
                data.field.createStartTime = searchDate[0];
                data.field.createEndTime = searchDate[1];
            }else{
                data.field.createStartTime = null;
                data.field.createEndTime = null;
            }
            data.field.updateDateRange = undefined;
            data.field.createDateRange = undefined;
            data.field.updateTimeRange = undefined;
            data.field.createTimeRange = undefined;
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });
    });
</script>
</body>
</html>